package com.oocl.euc.authentication.tool;

import com.oocl.euc.authentication.core.AuthUrlSetting;
import com.oocl.euc.common.constant.ApplicationConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Component
public class WebHelper {

    @Autowired
    private AuthUrlSetting urlSetting;

    public boolean isRest(HttpServletRequest request) {
        String servletPath = request.getServletPath();
        return (null != request.getHeader(ApplicationConstant.JWT_TOKEN_HEADER)) && (null != servletPath) && (servletPath.startsWith("/rest/"));
    }

    public boolean isSkip(HttpServletRequest request) {
        String servletPath = request.getServletPath();
        List<String> skipUrlPatterns = getUrlSetting().getSsoSkipUrlPatterns();
        for(String url : skipUrlPatterns) {
            int index = url.indexOf('*');
            if(index > -1){
                url = url.substring(0, index - 1);
            }
            if(servletPath.startsWith(url)){
                return true;
            }
        }
        return false;
    }

    protected AuthUrlSetting getUrlSetting() {
        return urlSetting;
    }
}
